<?php 
	function insert_tags($tag_name){
		$ci= &get_instance();
		$ci->load->helper("handle_string");
		$tags = explode(",",$tag_name);
		$return_slug = array();
		$insert_tags = array();
		
		foreach($tags as $i => $v){
			$v = trim($v);
			$slug = utf8_to_ascii($v);
			$insert_tags[$slug] = array("name"=>$v,"slug"=>$slug);
			$return_slug[$slug] = $slug;
			if($i != 0)
				$test_slug .= ",";
			$test_slug .= "'".$slug."'";
		}
		$return_slug = array_filter($return_slug);
		$insert_tags = array_filter($insert_tags);
		
		if(!empty($test_slug)){
			$list_tags_name = $ci->db->where("slug IN (".$test_slug.")")->get("tags")->result();
			if(!empty($list_tags_name)) foreach($list_tags_name as $item){
				unset($insert_tags[$item->slug]);
			}
		}
		if(!empty($insert_tags)){
			$ci->db->insert_batch("tags",$insert_tags);
		}
		return implode(",",$return_slug);
	} 
	function get_tags_name($tag_slug){
		if(!empty($tag_slug)){
			$tag_slug = "'".str_replace(",","','",$tag_slug)."'";
			$ci = &get_instance();
			return $ci->db->where("slug IN ($tag_slug)")->get("tags")->result();
		}
		return $tag_slug;
	}	
?>